-
-
Notifications
You must be signed in to change notification settings - Fork 503
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Allow specifying arguments to Cython cell_magic #38945
Allow specifying arguments to Cython cell_magic #38945
Conversation
0f3d239
to
ed00e96
Compare
Documentation preview for this PR (built with commit 5784784; changes) is ready! 🎉 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM.
Actually, looking at e.g. Is it worth it to change it to be consistent? On the other hand
and IPython (are you sure you don't mean to approve the other PR and leave the comment on documentation on this one instead?) |
I think we should be future-proof and
Either way is okay with me. Just let me know which one depends on which. |
For our simple arguments, are there differences in argument style between |
On the user side, it would looks mostly the same. But the code would look like this (i.e. more manual handling) boolean_options = {
"m": "compile-message",
"c": "use-cache",
"l": "create-local-c-file",
"a": "annotate",
"s": "sage-namespace",
"o": "create-local-so-file",
}
args, rest = self.parse_options(line, "v:" + "".join(boolean_options), "verbose=",
*boolean_options.values(),
*["no-" + k for k in boolean_options.values()])
from IPython.core.error import UsageError
if rest:
raise UsageError(f"unrecognized arguments: {rest}")
args_dict = dict(args)
if "v" in args_dict:
args_dict["verbose"] = args_dict["v"]
del args_dict["v"]
if "verbose" in args_dict:
args_dict["verbose"] = int(args_dict["verbose"])
for k in [*args_dict]:
if k in boolean_options:
args_dict[boolean_options[k]] = True
del args_dict[k]
elif k in boolean_options.values():
args_dict[k] = True
elif k.startswith("no-"):
args_dict[k[3:]] = False
del args_dict[k]
args_dict = {k.replace("-", "_"): v for k, v in args_dict.items()}
return cython_compile(cell, **args_dict) or args, rest = self.parse_options(
line, "v:mclaso", "verbose=",
"compile-message", "use-cache", "create-local-c-file", "annotate", "sage-namespace", "create-local-so-file",
"no-compile-message", "no-use-cache", "no-create-local-c-file", "no-annotate", "no-sage-namespace", "no-create-local-so-file",
)
from IPython.core.error import UsageError
if rest:
raise UsageError(f"unrecognized arguments: {rest}")
args_dict = {}
for k, v in args.items():
if k in ("v", "verbose"):
args_dict["verbose"] = int(v)
elif k in ("m", "compile-message"):
args_dict["compile_message"] = True
elif k in ("c", "use-cache"):
args_dict["use_cache"] = True
elif k in ("l", "create-local-c-file"):
args_dict["create_local_c_file"] = True
elif k in ("a", "annotate"):
args_dict["annotate"] = True
elif k in ("s", "sage-namespace"):
args_dict["sage_namespace"] = True
elif k in ("o", "create-local-so-file"):
args_dict["create_local_so_file"] = True
elif k.startswith("no-"):
args_dict[k[3:].replace("-", "_")] = False
else:
raise UsageError(f"unrecognized argument: {k}")
return cython_compile(cell, **args_dict) Only the error message would look different sage: %%cython -v1 --annotate --no-sage-namespace --xx
....: def f():
....: print('test')
....:
UsageError: option --xx not recognized ( allowed: "v:mclaso" verbose= compile-message use-cache crea
te-local-c-file annotate sage-namespace create-local-so-file no-compile-message no-use-cache no-crea
te-local-c-file no-annotate no-sage-namespace no-create-local-so-file) which aligns with existing IPython magics sage: %run -abc
UsageError: option -a not recognized ( allowed: "nidtN:b:pD:l:rs:T:em:G" ) |
I see. Aligning with existing ipython magics is good for consistency. Consistency is more visible to users. Hence I would value consistency more than future-proof. Anyway, I will leave the decision to you. |
I think there's also the part that using I don't think consistency is a big issue, now that I raise |
Done? LGTM. |
Can you change the label to positive-review then? Thanks. (not to try to bikeshedding the issue, but: do you think it's a good idea to specify all short options like that? It may take up space for other more sensical options later. I'd say |
Setting up a bike shed in a lot beside of a nuclear powerplant is perhaps a trivial thing. But in software engineering, I think small things are not trivial; they affect many people for a long time.
Alternatively, you may just give a couple of examples with useful options. |
Will you merge this branch to #38946? |
When you are done with this PR, you may set "positive review". |
will merge later when it's stable. No, what I mean is should short option exists for every long option --- for example Problem is once a short option is taken for something it cannot be taken for something else (which in retrospect might make more sense later) without like a year of deprecation so… I don't think I have permission to set review labels myself. |
OK.
I see. I agree that short option should exist only for frequent long options.
OK. Let me know when you are done. |
I realize I don't know which long option are common… but these 2 are definitely not common (they defaults to True). Chances are it doesn't matter that much anyway. I'm done with this. |
Actually… you can Should we just deprecate sage's at the very least I should delete the command-line arguments that conflicts with the cython version. |
Hmm I didn't know that (or completely forgot :-) Perhaps that is why sage's cython magic was not developed much by now.
I don't know. It all depends on how sage's cython magic compares with ipython's. Anyway a major merit of sage's cython magic is that it is available by default. Hence I think just removing it is a step backward.
Yes. We should align sage's cython magic with ipython's at least in user interface. As for this pr, for example, we should not define an argument with a different name but with the same function. This would confuse users. In the long run, sage's cython magic may be redefined as a wrapper of ipython's cython magic, loaded by default and with some additional conveniency arguments for sage users. |
That is easily bypassable by loading the extension at startup by Sage. Anyway it looks like the semantic of Sage's cell magic is a bit different from Cython's one (e.g. Sage's one suppress warnings by default) so yes we can just keep both, and maybe mention the existence of IPython's one somewhere in the documentation. |
As seen in the code, this allows user to write code like ``` %%cython --verbose=1 def f(): print(1) ``` and have the `verbose=1` passed to Cython compilation function. ### 📝 Checklist <!-- Put an `x` in all the boxes that apply. --> - [x] The title is concise and informative. - [x] The description explains in detail what this PR is about. - [x] I have linked a relevant issue or discussion. (not aware of one) - [x] I have created tests covering the changes. - [x] I have updated the documentation and checked the documentation preview. ------ Test failure is unrelated `https://github.com/sagemath/sage/issues/33906` (fixed in newest version) URL: sagemath#38945 Reported by: user202729 Reviewer(s): Kwankyu Lee
As seen in the code, this allows user to write code like ``` %%cython --verbose=1 def f(): print(1) ``` and have the `verbose=1` passed to Cython compilation function. ### 📝 Checklist <!-- Put an `x` in all the boxes that apply. --> - [x] The title is concise and informative. - [x] The description explains in detail what this PR is about. - [x] I have linked a relevant issue or discussion. (not aware of one) - [x] I have created tests covering the changes. - [x] I have updated the documentation and checked the documentation preview. ------ Test failure is unrelated `https://github.com/sagemath/sage/issues/33906` (fixed in newest version) URL: sagemath#38945 Reported by: user202729 Reviewer(s): Kwankyu Lee
I didn't know the dummy line is unnecessary while working on sagemath#38945 . Better removing it so that future developers won't stumble upon it in the future. Side note: ideally instead of `known bug (meson ...)` we could just do `optional - !meson`. ### 📝 Checklist <!-- Put an `x` in all the boxes that apply. --> - [x] The title is concise and informative. - [x] The description explains in detail what this PR is about. - [x] I have linked a relevant issue or discussion. - [ ] I have updated the documentation and checked the documentation preview. ### ⌛ Dependencies <!-- List all open PRs that this PR logically depends on. For example, --> <!-- - sagemath#12345: short description why this is a dependency --> <!-- - sagemath#34567: ... --> URL: sagemath#39114 Reported by: user202729 Reviewer(s): Kwankyu Lee
…aining the annotated HTML file As in the title. I think this is a rather common use case, to investigate whether the code does get the desired speedup. Remark: there's `sage.misc.viewer.browser()` which could be used instead, but then a lot of existing code in SageMath uses `webbrowser.open()` anyway? ### 📝 Checklist <!-- Put an `x` in all the boxes that apply. --> - [x] The title is concise and informative. - [x] The description explains in detail what this PR is about. - [x] I have linked a relevant issue or discussion. (not aware of one) - [x] I have created tests covering the changes. - [x] I have updated the documentation and checked the documentation preview. ### Dependencies Depends on sagemath#38945 . URL: sagemath#38946 Reported by: user202729 Reviewer(s): Kwankyu Lee, user202729
…aining the annotated HTML file As in the title. I think this is a rather common use case, to investigate whether the code does get the desired speedup. Remark: there's `sage.misc.viewer.browser()` which could be used instead, but then a lot of existing code in SageMath uses `webbrowser.open()` anyway? ### 📝 Checklist <!-- Put an `x` in all the boxes that apply. --> - [x] The title is concise and informative. - [x] The description explains in detail what this PR is about. - [x] I have linked a relevant issue or discussion. (not aware of one) - [x] I have created tests covering the changes. - [x] I have updated the documentation and checked the documentation preview. ### Dependencies Depends on sagemath#38945 . URL: sagemath#38946 Reported by: user202729 Reviewer(s): Kwankyu Lee, user202729
As seen in the code, this allows user to write code like
and have the
verbose=1
passed to Cython compilation function.📝 Checklist
Test failure is unrelated
https://github.com/sagemath/sage/issues/33906
(fixed in newest version)